package com.meida.student.app.user;

import com.meida.app.provider.handler.UserInfoHandler;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.module.bus.client.enums.BalanceLogTypeEnum;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class UserRegHandler implements UserInfoHandler {
    @Autowired
    private AppUserService userService;
    @Autowired
    private PayBalanceService payBalanceService;

    @Override
    public AppUser bindMobile(AppUser baseUser) {
        Long userId = baseUser.getUserId();
        AppUser appUser = userService.getById(userId);
        //推荐人ID
        Long inviterId = appUser.getInviterId();
        if (FlymeUtils.isNotEmpty(inviterId)) {
            giveAmount(userId, inviterId);
        }
        return baseUser;
    }

    @Override
    public void userReg(CriteriaSave cs) {
        AppUser baseUser = cs.getEntity(AppUser.class);
        Long userId = baseUser.getUserId();
        AppUser appUser = userService.getById(userId);
        //推荐人ID
        Long inviterId = appUser.getInviterId();
        if (FlymeUtils.isNotEmpty(inviterId)) {
            giveAmount(userId, inviterId);
        }
        appUser.setPushOrderMsg(1);
        appUser.setPushOtherMsg(1);
        appUser.setPushSysMsg(1);
        appUser.setBusNum(3);
        appUser.updateById();
    }

    void giveAmount(Long userId, Long inviterId) {
        BigDecimal amount = new BigDecimal(10);
        //推荐人
        String title = "邀请奖励";
        String body = "邀请奖励发放";
        payBalanceService.addBalance(userId, "", amount, "", BalanceLogTypeEnum.logType6.getValue(), null, title, body);
        //被推荐人
        payBalanceService.addBalance(inviterId, "", amount, "", BalanceLogTypeEnum.logType6.getValue(), null, title, body);
    }
}
